package com.ifast.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ifast.common.domain.DictDO;
import com.ifast.common.domain.ExportDemo;
import com.ifast.common.service.DictService;
import com.ifast.common.utils.Const;
import com.ifast.sys.domain.DeptDO;
import com.ifast.sys.domain.UserDO;
import com.ifast.sys.service.DeptAttrService;
import com.ifast.sys.service.DeptService;
import com.ifast.sys.service.UserService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * </pre>
 * <small> 2018年3月23日 | Aron</small>
 */
@Transactional
@Service
public class DeptAttrServiceImpl implements DeptAttrService {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DictService dictService;

    @Autowired
    private UserService userService;

    @Override
    public void updateDept(){
        DeptDO deptDORoot = this.deptService.selectById("1");
        deptDORoot.setHasChild(1);
        deptDORoot.setDwCode("1000");
        this.deptService.updateById(deptDORoot);
        this.ClChildUpdateDept(deptDORoot);
    }

    @Override
    public void updateUserCode(){
        List<UserDO> list = this.userService.selectList(null);
        for (UserDO userDO : list) {
            DeptDO deptDO = this.deptService.selectById(userDO.getDeptId());
            if(deptDO != null){
                userDO.setDwCode(deptDO.getDwCode());
            }
        }
        this.userService.updateBatchById(list);
    }


    public void ClChildUpdateDept(DeptDO parentDeptDO){
        Wrapper<DeptDO> wrapper = new EntityWrapper<DeptDO>();
        wrapper.eq("parentId",parentDeptDO.getId());
        List<DeptDO> listChild = this.deptService.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(listChild)){
            for (DeptDO deptDOC : listChild) {
                //本级最大code值
                DeptDO deptDOCode = this.deptService.selectMaxCodeByPid(deptDOC);
                if(deptDOCode == null){
                    String dwCodep = parentDeptDO.getDwCode();
                    deptDOC.setDwCode(dwCodep+"1000");
                }else{
                    String hzCode = deptDOCode.getDwCode().substring(deptDOCode.getDwCode().length()-4,deptDOCode.getDwCode().length());
                    String qzCode = deptDOCode.getDwCode().substring(0,deptDOCode.getDwCode().length()-4);
                    deptDOC.setDwCode(qzCode+(Integer.valueOf(hzCode)+1)+"");
                }
                //是否有下级
                Wrapper<DeptDO> wrapper1 = new EntityWrapper<DeptDO>();
                wrapper1.eq("parentId",deptDOC.getId());
                int countz = this.deptService.selectCount(wrapper1);
                if(countz > 0){
                    deptDOC.setHasChild(1);
                }else{
                    deptDOC.setHasChild(0);
                }
                this.deptService.updateById(deptDOC);
                if (countz > 0) {
                    this.ClChildUpdateDept(deptDOC);
                }
            }
        }
    }

    public static void main(String[] args) {
        String code = "000100020003";
        String hzCode = code.substring(code.length()-4,code.length());
        String qzCode = code.substring(0,code.length()-4);
        System.out.println(hzCode);
        System.out.println(qzCode);
    }

    /**
     * 导入
     * @param exportDemoList
     */
    @Override
    public void saveDeptAndAttr(List<ExportDemo> exportDemoList){
        Wrapper<DictDO> wrapper = new EntityWrapper<>();
        wrapper.like("value","dw_");
        wrapper.isNull("parentId");
        List<DictDO> dictDOList = this.dictService.selectList(wrapper);
        Map<String,DictDO> mapDicPNames = Maps.newHashMap();
        for (DictDO dictDO : dictDOList) {
            mapDicPNames.put(dictDO.getName(),dictDO);
        }

        List<DeptDO> deptDOInsertList = Lists.newArrayList();
        Map<String,String> mapExcelNames = Maps.newHashMap();
        int i = 1;
        for (ExportDemo exportDemo : exportDemoList) {
            if(StrUtil.isNotBlank(exportDemo.getQymc())){
                if(!mapExcelNames.containsKey(exportDemo.getQymc())){
                    String parentId = "1";
                    DeptDO deptDO = new DeptDO();
                    deptDO.setId(Const.uuid());
                    deptDO.setDelFlag(0);
                    deptDO.setName(exportDemo.getQymc());
                    deptDO.setOrderNum(i+1);
                    deptDO.setParentId(parentId);
                    deptDO.setYb(exportDemo.getYb());
                    deptDO.setScjydz(exportDemo.getScjydz());
                    deptDO.setZjdyq(exportDemo.getZjdyq());
                    deptDO.setZyfzr(exportDemo.getZyfzr());
                    deptDO.setZyjgbm(exportDemo.getZyjgbm());
                    Wrapper<DictDO> wrapper1 = new EntityWrapper<>();
                    if(StrUtil.isNotBlank(exportDemo.getJjlx())){
                        wrapper1.eq("name",exportDemo.getJjlx());
                        DictDO dictDOOld = this.dictService.selectOne(wrapper1);
                        if(dictDOOld == null){
                            DictDO dictDONew = this.saveDic(exportDemo.getJjlx(),mapDicPNames.get("经济类型"));
                            deptDO.setJjlxId(dictDONew.getId());
                        }
                    }
                    if(StrUtil.isNotBlank(exportDemo.getQyjyzt())){
                        wrapper1 = new EntityWrapper<>();
                        wrapper1.eq("name",exportDemo.getQyjyzt());
                        DictDO dictDOOld = this.dictService.selectOne(wrapper1);
                        if(dictDOOld == null){
                            DictDO dictDONew = this.saveDic(exportDemo.getQyjyzt(),mapDicPNames.get("企业经营状态"));
                            deptDO.setQyjyztId(dictDONew.getId());
                        }else{
                            deptDO.setQyjyztId(dictDOOld.getId());
                        }
                    }
                    if(StrUtil.isNotBlank(exportDemo.getQygm())){
                        wrapper1 = new EntityWrapper<>();
                        wrapper1.eq("name",exportDemo.getQygm());
                        DictDO dictDOOld = this.dictService.selectOne(wrapper1);
                        if(dictDOOld == null){
                            DictDO dictDONew = this.saveDic(exportDemo.getQygm(),mapDicPNames.get("企业规模"));
                            deptDO.setQygmId(dictDONew.getId());
                        }else{
                            deptDO.setQygmId(dictDOOld.getId());
                        }
                    }
                    if(StrUtil.isNotBlank(exportDemo.getFxdj())){
                        wrapper1 = new EntityWrapper<>();
                        wrapper1.eq("name",exportDemo.getFxdj());
                        DictDO dictDOOld = this.dictService.selectOne(wrapper1);
                        if(dictDOOld == null){
                            DictDO dictDONew = this.saveDic(exportDemo.getFxdj(),mapDicPNames.get("风险等级"));
                            deptDO.setFxdjId(dictDONew.getId());
                        }else{
                            deptDO.setFxdjId(dictDOOld.getId());
                        }
                    }
                    if(StrUtil.isNotBlank(exportDemo.getHylb())){
                        wrapper1 = new EntityWrapper<>();
                        wrapper1.eq("name",exportDemo.getHylb());
                        DictDO dictDOOld = this.dictService.selectOne(wrapper1);
                        if(dictDOOld == null){
                            DictDO dictDONew = this.saveDic(exportDemo.getHylb(),mapDicPNames.get("行业类别"));
                            deptDO.setQylbId(dictDONew.getId());
                        }else{
                            deptDO.setQylbId(dictDOOld.getId());
                        }
                    }
                    deptDOInsertList.add(deptDO);
                    mapExcelNames.put(exportDemo.getQymc(),exportDemo.getQymc());
                }
            }
        }
        //初始化单位
        this.deptService.insertBatch(deptDOInsertList);
    }


    public DictDO saveDic(String dictname,DictDO dictDOParent){
        DictDO dictDO = new DictDO();
        dictDO.setDelFlag("0");
        dictDO.setName(dictname);
        dictDO.setValue(Const.uuid());
        dictDO.setRemarks("");
        dictDO.setDescription("");
        dictDO.setType(dictDOParent.getValue());
        dictDO.setParentId(dictDOParent.getId());
        dictDO.setSort(BigDecimal.ONE);
        this.dictService.insert(dictDO);
        return  dictDO;
    }
}
